﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFByCoreApp
{
    /// <summary>
    /// Interaction logic for TestSaveFileDialogWindow.xaml
    /// </summary>
    public partial class TestSaveFileDialogWindow : Window
    {



        public TestSaveFileDialogWindow()
        {
            InitializeComponent();

            this.Loaded += TestSaveFileDialogWindow_Loaded;
            this.Unloaded += TestSaveFileDialogWindow_Unloaded;
            this.PreviewKeyDown += TestSaveFileDialogWindow_PreviewKeyDown;

        }

        private void TestSaveFileDialogWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WHwnd.GetWindowHwndSource(this);

            WHwnd.Hwnd.AddHook(HwndSourceHookCallBack);

        }

        private void TestSaveFileDialogWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            WHwnd.Hwnd.RemoveHook(HwndSourceHookCallBack);
        }


        private void TestSaveFileDialogWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine("TestSaveFileDialogWindow_PreviewKeyDown-----Key:" + (e.Key == Key.ImeProcessed ? e.ImeProcessedKey : e.SystemKey).ToString() + ";时间:" + DateTime.Now);
        }



        public IntPtr HwndSourceHookCallBack(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {

                case (int)VirtualKeyCodeEnum.WM_SYSKEYUP:
                case (int)VirtualKeyCodeEnum.WM_KEYUP:
                    //KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
                    //Keys k = (Keys)Enum.Parse(typeof(Keys), kbh.vkCode.ToString());
                    Keys k = (Keys)Enum.Parse(typeof(Keys), wParam.ToString());
                    Debug.WriteLine("HwndSourceHookCallBack---hwnd:" + hwnd + "--key:" + k + "--lParam:" + lParam);

                    //switch (k)
                    //{

                    //    case Keys.Up:
                    //    case Keys.Down:
                    //        KeyCharTool.keybd_event((int)Keys.Tab, 0, 0, 0);
                    //        KeyCharTool.keybd_event((int)Keys.Tab, 0, 0x02, 0);
                    //        //handled = true;
                    //        break;
                    //}

                    break;
            }

            return IntPtr.Zero;
        }



        private void btnSaveFileDialog_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
            try
            {
                saveFileDialog.FileName = "test.txt";
                saveFileDialog.InitialDirectory = "c:\\";
                saveFileDialog.CheckFileExists = false;
                saveFileDialog.CheckPathExists = false;
                saveFileDialog.OverwritePrompt = false;
                KeyPress_o.Hook_Start();

                saveFileDialog.ShowDialog();

                KeyPress_o.Hook_Clear();
            }
            catch (Exception)
            {

            }
        }

        private void btnTestSendMsg_Click(object sender, RoutedEventArgs e)
        {
            KeyCharTool.SendMessage(WHwnd.Hwnd.Handle, (int)VirtualKeyCodeEnum.WM_SYSKEYDOWN, 0X09, 0);
            KeyCharTool.SendMessage(WHwnd.Hwnd.Handle, (int)VirtualKeyCodeEnum.WM_SYSKEYUP, 0X09, 0);
        }

        private void btnMyFileDialog_Click(object sender, RoutedEventArgs e)
        {
            //MySaveFileDialog mySaveFileDialog = new MySaveFileDialog();
            //mySaveFileDialog.ShowDialog();
        }
    }

    public class WHwnd
    {
        /// <summary>
        /// 主窗体句柄
        /// </summary>
        public static System.Windows.Interop.HwndSource Hwnd;
        /// <summary>
        /// 获取窗体句柄
        /// </summary>
        /// <param name="window">窗体</param>
        public static IntPtr GetWindowHwndSource(DependencyObject window, bool isHwnd = true)
        {
            var formDependency = System.Windows.Interop.HwndSource.FromDependencyObject(window);
            System.Windows.Interop.HwndSource winformWindow = (formDependency as System.Windows.Interop.HwndSource);
            if (isHwnd)
                Hwnd = winformWindow;
            return winformWindow.Handle;
        }

    }




    //public class MySaveFileDialog : FileDialog
    //{

    //    protected override IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
    //    {
    //        switch (msg)
    //        {

    //            case (int)VirtualKeyCodeEnum.WM_SYSKEYUP:
    //            case (int)VirtualKeyCodeEnum.WM_KEYUP:



    //                Keys k = (Keys)Enum.Parse(typeof(Keys), wParam.ToString());
    //                Debug.WriteLine("FileDialog-HookProc---hwnd:" + hwnd + "--key:" + k + "--lParam:" + lParam);

    //                //switch (k)
    //                //{

    //                //    case Keys.Up:
    //                //    case Keys.Down:
    //                //        KeyCharTool.keybd_event((int)Keys.Tab, 0, 0, 0);
    //                //        KeyCharTool.keybd_event((int)Keys.Tab, 0, 0x02, 0);
    //                //        //handled = true;
    //                //        break;
    //                //}

    //                break;
    //        }



    //        return base.HookProc(hwnd, msg, wParam, lParam);
    //    }


    //    protected override bool RunDialog(IntPtr hwndOwner)
    //    {
    //        return base.RunDialog(hwndOwner);
    //    }

    //    public override bool? ShowDialog()
    //    {
    //        return base.ShowDialog();
    //    }
    //}
}
